package com.logistics.receipt;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.logistics.database.Receiptdatabase;

public class receiptservlet extends HttpServlet {

	/**
	 * Constructor of the object.
	 */
	public receiptservlet() {
		super();
	}

	/**
	 * Destruction of the servlet. <br>
	 */
	public void destroy() {
		super.destroy(); // Just puts "destroy" string in log
		// Put your code here
	}

	/**
	 * The doGet method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to get.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doPost(request, response);
	}

	/**
	 * The doPost method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to post.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		response.setContentType("text/xml;charset=UTF-8");
		request.setCharacterEncoding("utf-8");
		Receiptdatabase dao= new Receiptdatabase();
		ResultSet rs;
		
		if("insert".equals(request.getParameter("operation")))
		{
			Receiptdatabase dao2= new Receiptdatabase();
			dao.ReceiptInsert(
					Integer.valueOf(request.getParameter("txt_mission_id")), 
					dao2.ReceiptGetOrderId(Integer.valueOf(request.getParameter("txt_mission_id"))),
					Integer.valueOf(request.getParameter("txt_substation_id")), 
					request.getParameter("txt_mission_type"), 
					request.getParameter("txt_address"),
					request.getParameter("txt_user_name"),
					request.getParameter("txt_product_name"), 
					Integer.valueOf(request.getParameter("txt_product_number")), 
					Double.valueOf(request.getParameter("txt_product_price")), 
					request.getParameter("txt_tips") );
			request.getRequestDispatcher("Receiptservlet_query").forward(request,response);
			return;
		}
		
		
		ArrayList<Integer> array=new ArrayList<Integer>();
		rs=dao.ReceiptGetMissionId();
		try {
			while(rs.next())
			{
				Integer temp;
				temp=rs.getInt("mission_id");
				array.add(temp);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		if("refresh".equals(request.getParameter("operation")))
		{
			receiptInfo sel=new receiptInfo();
			rs=dao.ReceiptMissionQuery(Integer.valueOf(request.getParameter("txt_mission_id")));
			try {
				if(rs.next())
				{
					Receiptdatabase dao2= new Receiptdatabase();
					sel.setMission_id(rs.getInt("mission_id"));
					sel.setSubstation_id(rs.getInt("substation_id"));
					sel.setMission_type(rs.getString("mission_type"));
					sel.setAddress(rs.getString("address"));
					sel.setUser_name(rs.getString("user_name"));
					sel.setProduct_name(dao2.receiptMissionGetProductName(rs.getInt("product_id")));
					sel.setProduct_number(rs.getInt("product_number"));
					sel.setProduct_price(dao2.receiptMissionGetProductPrice(rs.getInt("product_id")));
				}
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			request.setAttribute("sel", sel);
		}
		if(array.isEmpty())
		{
			request.setAttribute("arr", null);
			System.out.println("eee");
		}
		else
		{
			request.setAttribute("arr", array);
			System.out.println("xxx");
		}
		request.getRequestDispatcher("../receipt/receipt_init.jsp").forward(request,response);
		
	}

	/**
	 * Initialization of the servlet. <br>
	 *
	 * @throws ServletException if an error occurs
	 */
	public void init() throws ServletException {
		// Put your code here
	}

}
